/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.bsn.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import org.hutech09.jsale.bsn.IOrderManager;
import org.hutech09.jsale.bsn.common.DataExistsException;
import org.hutech09.jsale.bsn.common.IGenericDao;
import org.hutech09.jsale.entity.Customer;
import org.hutech09.jsale.entity.OrderDetail;
import org.hutech09.jsale.entity.POrder;

/**
 *
 * @author Nam
 */
@Stateless
public class OrderManager implements IOrderManager {

    @EJB
    private IGenericDao dao;

    @Override
    public POrder createOrder(POrder order) {
        if (order == null) {
            throw new IllegalArgumentException("order can't be null");
        }
        try {
            return dao.create(order);
        } catch (DataExistsException ex) {
            Logger.getLogger(OrderManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public OrderDetail createOrderDetail(OrderDetail od) {
        if (od == null) {
            throw new IllegalArgumentException("od can't be null");
        }
        try {
            return dao.create(od);
        } catch (DataExistsException ex) {
            Logger.getLogger(OrderManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public List<OrderDetail> getOrderDetail(POrder order) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("pOrder", order);
        return dao.findByNamedQuery("OrderDetail.findByOrder", map);
    }

    @Override
    public List<POrder> getOrders(Customer customer) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("customer", customer);
        return dao.findByNamedQuery("POrder.findByCustomer", map);
    }
}
